<template>
  <div>
    <el-card style="width: 50%">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px" style="padding-right: 50px">
        <el-form-item prop="email" label="您的邮箱：">
          <el-input prefix-icon="el-icon-lock" placeholder="请输入邮箱" :value="this.email" v-model="form.email" disabled></el-input>
<!--          <el-button class="btn-orange" style="margin-right: -2px" :plain="true" :disabled="disabled"
                     @click="getcode">{{ valiBtn }}
          </el-button>-->
        </el-form-item>
        <el-form-item prop="code" label="验证码" >
          <el-row :gutter="3">
            <el-col :span="18">
              <el-input placeholder="请输入验证码" v-model="form.code">
                <i slot="prefix" class="el-icon-tickets"></i>
              </el-input>
            </el-col>
            <el-col :span="2">
              <el-button @click="getcode" :disabled="disabled">{{valiBtn}}</el-button>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input show-password v-model="form.newPassword" placeholder="新密码"></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="confirmPassword">
          <el-input show-password v-model="form.confirmPassword" placeholder="确认密码"></el-input>
        </el-form-item>
        <div style="text-align: center; margin-bottom: 20px">
          <el-button type="primary" @click="update">确认修改</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "Password",
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请确认密码'))
      } else if (value !== this.form.newPassword) {
        callback(new Error('确认密码错误'))
      } else {
        callback()
      }
    }

    return {
      form: {
      },
      disabled: false,
      valiBtn: '获取验证码',
      email: '',
      code: "",
      username: "",
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
      rules: {
        password: [
          {required: true, message: '请输入原始密码', trigger: 'blur'},
        ],
        newPassword: [
          {required: true, message: '请输入新密码', trigger: 'blur'},
        ],
        code: [
          {required: true, message: '请输入验证码', trigger: 'blur'},
        ],
        confirmPassword: [
          {validator: validatePassword, required: true, trigger: 'blur'},
        ],
        email: [
          {required: true, message: '请输入邮箱地址', trigger: 'blur'},
          {type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change']}
        ]
      }
    }
  },
  created() {


  },
  mounted() {
    this.currentUser()
  },
  methods: {
    update() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.$request.put('/updatePassword', {
            username:this.username,
            email: this.form.email,
            newPassword: this.form.newPassword,
            code:this.form.code
          }).then(res => {
            if (res.code === 0) {
              // 成功更新
              localStorage.removeItem('xm-user')   // 清除缓存的用户信息
              this.$message.success('修改密码成功')
              this.$router.push('/login')
            } else {
              this.$message.error(res.message)
            }
          })
        }
      })
    },
    getcode() {
      let time = 60;
      const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
      console.log(emailRegex.test(this.form.email))
      if (this.form.email === "" || emailRegex.test(this.form.email) === false) {
        this.$message('请输入正确的邮箱');
      } else {

        this.$request.get(`/sendEmail/${this.email}`, {}).then(res => {
          console.log(res)
          if (res.code === 0) {
            this.$message.success('发送成功')
          } else {
            this.$message.error(res.message)
          }
        })
      }
      let timer = setInterval(() => {
        if (time === 0) {
          clearInterval(timer);
          this.valiBtn = '获取验证码';
          this.disabled = false;
        } else {
          this.disabled = true;
          this.valiBtn = time + '秒后重试';
          time--;
        }
      }, 1000);
    },
    currentUser() {
      this.$request.get('/current').then(res => {
        if (res.code === 0) {
          this.$message.success('获取用户成功')
          this.username = res.data.username,
              this.email = res.data.email
          this.form.email = res.data.email
          console.log(this.username)
          console.log(this.email)
        } else {
          this.$message.error(res.message)
        }
      })
    },


  }



}
</script>

<style scoped>
/deep/ .el-form-item__label {
  font-weight: bold;
}
</style>
